import 'dart:ui';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:riverpod_t/base/base_async_consumer.dart';
import 'package:riverpod_t/page/car/data/model/info_model.dart';
import 'package:riverpod_t/page/car/event/car_info_op_event.dart';
import 'package:riverpod_t/page/car/river/car_info_op_river.dart';
import 'package:riverpod_t/page/car/state/car_op_state.dart';
import 'package:riverpod_t/routers/navigator_utils.dart';
import 'package:riverpod_t/widget/custom_button.dart';
import '../../../until/app_util.dart';

class CommonBottomDialog extends StatelessWidget {
  final String? title;
  final String? content;
  final InfoModel model;
  final VoidCallback? cancel;
  final VoidCallback? confirm;
  const CommonBottomDialog({
    super.key,
    this.title,
    this.content,
    this.cancel,
    this.confirm,
    required this.model,
  });

  /// 标题
  Widget get _titleWidget => Padding(
    padding: EdgeInsets.symmetric(vertical: 12.h),
    child: Text(
      title ?? '提示',
      style: TextStyle(fontSize: 18.sp, color: Color(0xFF000000), fontWeight: FontWeight.w500),
    ),
  );

  /// 描述
  Widget get _contentWidget => Padding(
    padding: EdgeInsets.symmetric(vertical: 12.h),
    child: Text(
      content ?? '暂无描述',
      style: TextStyle(fontSize: 14.sp, color: Color(0xFF666666), fontWeight: FontWeight.w400),
    ),
  );

  /// 取消按钮
  Widget get _cancelBtn => Expanded(
    child: CustomButton(
      margin: EdgeInsets.only(top: 12.h),
      text: '取消',
      height: 35.h,
      borderRadius: 20.h,
      borderColor: Colors.blue,
      borderWidth: 1.w,
      textColor: Colors.blue,
      onPressed: () {
        if (AppUtil.getContext() != null) {
          NavigatorUtils.pop(AppUtil.getContext()!);
          if (cancel != null) cancel!.call();
        }
      },
    ),
  );

  /// 确认按钮
  Widget get _confirmBtn => Expanded(
    child: BaseAsyncConsumer<CarInfoOpState>(
      provider: carInfoOpRiverProvider,
      listenStateChange: (context, p, n) => _listenListChange(context, p, n),
      builder: (BuildContext context, CarInfoOpState state, WidgetRef ref) {
        return CustomButton(
          margin: EdgeInsets.only(top: 12.h),
          text: '确认',
          height: 35.h,
          backgroundColor: Colors.blue,
          textColor: Colors.white,
          onPressed: () {
            ref.read(carInfoOpRiverProvider.notifier).removeCarInfo(CarInfoRemoveOp(model: model));
          },
        );
      },
    ),
  );

  void _listenListChange(
    BuildContext context,
    AsyncValue<CarInfoOpState>? previous,
    AsyncValue<CarInfoOpState>? next,
  ) {
    if (next?.value?.op is CarInfoRemoveOp && next?.value?.isSuccess == true) {
      NavigatorUtils.pop(context);
    }
  }

  /// 横向间距
  Widget _horizontalSize(double customWidth) {
    return SizedBox(width: customWidth);
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) {
        return SingleChildScrollView(
          child: ConstrainedBox(
            constraints: BoxConstraints(
              minHeight: constraints.maxHeight * 0.4,
              maxHeight: constraints.maxHeight, // 不超过屏幕90%
            ),
            child: IntrinsicHeight(
              child: Container(
                alignment: Alignment.center,
                padding: EdgeInsets.only(
                  left: 18.w,
                  right: 18.w,
                  top: 12.h,
                  bottom: 12.h + ScreenUtil().bottomBarHeight,
                ),
                decoration: BoxDecoration(color: Colors.white),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    _titleWidget,
                    Expanded(child: _contentWidget),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [_cancelBtn, _horizontalSize(20.w), _confirmBtn],
                    ),
                  ],
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}
